#include "CustomMessageBox.h"
#include <QApplication>
#include <QPainter>
#include <QFontMetrics>
#include <QScreen>
#include <QMouseEvent>
#include <QDebug>

CustomMessageBox::CustomMessageBox(QWidget *parent)
    : QDialog(parent)
    , m_mainFrame(nullptr)
    , m_mainLayout(nullptr)
    , m_buttonLayout(nullptr)
    , m_titleLabel(nullptr)
    , m_textLabel(nullptr)
    , m_okButton(nullptr)
    , m_cancelButton(nullptr)
    , m_yesButton(nullptr)
    , m_noButton(nullptr)
    , m_closeButton(nullptr)
    , m_iconType(NoIcon)
    , m_buttons(Ok)
    , m_defaultButton(Ok)
    , m_dragging(false)
{
    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setModal(true);
    
    setupUI();
    setupConnections();
}

CustomMessageBox::~CustomMessageBox()
{
}

void CustomMessageBox::setupUI()
{
    // 主框架
    m_mainFrame = new QFrame(this);
    m_mainFrame->setObjectName("customMessageBox");
    m_mainFrame->setStyleSheet(R"(
        QFrame#customMessageBox {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                                        stop:0 rgba(10, 46, 91, 0.98), 
                                        stop:1 rgba(5, 9, 28, 0.95));
            border: 2px solid #007AED;
            border-radius: 8px;
        }
    )");
    
    // 添加阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect();
    shadowEffect->setBlurRadius(15);
    shadowEffect->setColor(QColor(0, 0, 0, 80));
    shadowEffect->setOffset(0, 3);
    m_mainFrame->setGraphicsEffect(shadowEffect);
    
    // 主布局
    m_mainLayout = new QVBoxLayout(m_mainFrame);
    m_mainLayout->setContentsMargins(20, 16, 20, 16);
    m_mainLayout->setSpacing(20);
    
    // 文本区域
    QVBoxLayout *textLayout = new QVBoxLayout();
    textLayout->setSpacing(12);
    
    // 标题标签
    m_titleLabel = new QLabel();
    m_titleLabel->setStyleSheet(R"(
        QLabel {
            color: #FFFFFF;
            font-size: 16px;
            font-weight: bold;
            background-color: transparent;
            padding: 0px;
        }
    )");
    m_titleLabel->setAlignment(Qt::AlignCenter);
    
    // 文本标签
    m_textLabel = new QLabel();
    m_textLabel->setStyleSheet(R"(
        QLabel {
            color: #BCD7FF;
            font-size: 13px;
            font-weight: medium;
            background-color: transparent;
            line-height: 1.5;
        }
    )");
    m_textLabel->setWordWrap(true);
    m_textLabel->setTextFormat(Qt::PlainText);
    m_textLabel->setAlignment(Qt::AlignCenter);
    
    textLayout->addWidget(m_titleLabel);
    textLayout->addWidget(m_textLabel);
    
    // 按钮布局
    m_buttonLayout = new QHBoxLayout();
    m_buttonLayout->setSpacing(10);
    m_buttonLayout->addStretch();
    
    // 创建按钮
    m_okButton = new QPushButton("确定");
    m_cancelButton = new QPushButton("取消");
    m_yesButton = new QPushButton("是");
    m_noButton = new QPushButton("否");
    m_closeButton = new QPushButton("关闭");
    
    // 设置按钮样式
    QString buttonStyle = R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                                        stop:0 #2B80FF, stop:1 #1A6BFF);
            color: #FFFFFF;
            border: 1px solid #007AED;
            border-radius: 4px;
            padding: 8px 16px;
            min-width: 70px;
            font-size: 13px;
            font-weight: medium;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                                        stop:0 #1A6BFF, stop:1 #0F5AE6);
            border-color: #45BFFF;
        }
        QPushButton:pressed {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                                        stop:0 #0F5AE6, stop:1 #0A4DCC);
            border-color: #2B80FF;
        }
        QPushButton:default {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                                        stop:0 #45BFFF, stop:1 #2B80FF);
            border-color: #45BFFF;
        }
    )";
    
    m_okButton->setStyleSheet(buttonStyle);
    m_cancelButton->setStyleSheet(buttonStyle);
    m_yesButton->setStyleSheet(buttonStyle);
    m_noButton->setStyleSheet(buttonStyle);
    m_closeButton->setStyleSheet(buttonStyle);
    
    // 按钮后添加stretch以实现居中
    m_buttonLayout->addStretch();
    
    // 添加到布局
    m_mainLayout->addLayout(textLayout);
    m_mainLayout->addLayout(m_buttonLayout);
    
    // 设置对话框布局
    QVBoxLayout *dialogLayout = new QVBoxLayout(this);
    dialogLayout->setContentsMargins(0, 0, 0, 0);
    dialogLayout->addWidget(m_mainFrame);
    
    // 设置默认大小
    resize(350, 140);
}

void CustomMessageBox::setupConnections()
{
    connect(m_okButton, &QPushButton::clicked, this, &CustomMessageBox::onButtonClicked);
    connect(m_cancelButton, &QPushButton::clicked, this, &CustomMessageBox::onButtonClicked);
    connect(m_yesButton, &QPushButton::clicked, this, &CustomMessageBox::onButtonClicked);
    connect(m_noButton, &QPushButton::clicked, this, &CustomMessageBox::onButtonClicked);
    connect(m_closeButton, &QPushButton::clicked, this, &CustomMessageBox::onButtonClicked);
}

void CustomMessageBox::setTitle(const QString &title)
{
    m_titleLabel->setText(title);
}

void CustomMessageBox::setText(const QString &text)
{
    m_textLabel->setText(text);
}

void CustomMessageBox::setIcon(IconType icon)
{
    m_iconType = icon;
    // 不显示图标，保持简洁
}

void CustomMessageBox::setButtons(int buttons)
{
    m_buttons = buttons;
    updateButtonLayout();
}

void CustomMessageBox::setDefaultButton(ButtonType button)
{
    m_defaultButton = button;
    
    // 清除所有按钮的默认状态
    m_okButton->setDefault(false);
    m_cancelButton->setDefault(false);
    m_yesButton->setDefault(false);
    m_noButton->setDefault(false);
    m_closeButton->setDefault(false);
    
    // 设置新的默认按钮
    switch (button) {
        case Ok: m_okButton->setDefault(true); break;
        case Cancel: m_cancelButton->setDefault(true); break;
        case Yes: m_yesButton->setDefault(true); break;
        case No: m_noButton->setDefault(true); break;
        case Close: m_closeButton->setDefault(true); break;
    }
}

void CustomMessageBox::updateButtonLayout()
{
    // 清除现有按钮
    QLayoutItem *item;
    while ((item = m_buttonLayout->takeAt(0)) != nullptr) {
        delete item->widget();
        delete item;
    }
    
    m_buttonLayout->addStretch();
    
    // 根据按钮类型添加按钮
    if (m_buttons & Ok) {
        m_buttonLayout->addWidget(m_okButton);
    }
    if (m_buttons & Cancel) {
        m_buttonLayout->addWidget(m_cancelButton);
    }
    if (m_buttons & Yes) {
        m_buttonLayout->addWidget(m_yesButton);
    }
    if (m_buttons & No) {
        m_buttonLayout->addWidget(m_noButton);
    }
    if (m_buttons & Close) {
        m_buttonLayout->addWidget(m_closeButton);
    }
    
    // 按钮后添加stretch以实现居中
    m_buttonLayout->addStretch();
}

QString CustomMessageBox::getIconText(IconType icon) const
{
    switch (icon) {
        case Information: return "ℹ";
        case Warning: return "⚠";
        case Critical: return "✕";
        case Question: return "?";
        case NoIcon:
        default: return "";
    }
}

QColor CustomMessageBox::getIconColor(IconType icon) const
{
    switch (icon) {
        case Information: return QColor("#45BFFF");
        case Warning: return QColor("#FFA500");
        case Critical: return QColor("#FF4444");
        case Question: return QColor("#45BFFF");
        case NoIcon:
        default: return QColor("#45BFFF");
    }
}

void CustomMessageBox::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    ButtonType buttonType = Ok;
    
    if (button == m_okButton) buttonType = Ok;
    else if (button == m_cancelButton) buttonType = Cancel;
    else if (button == m_yesButton) buttonType = Yes;
    else if (button == m_noButton) buttonType = No;
    else if (button == m_closeButton) buttonType = Close;
    
    emit buttonClicked(buttonType);
    done(static_cast<int>(buttonType));
}

void CustomMessageBox::paintEvent(QPaintEvent *event)
{
    QDialog::paintEvent(event);
}

void CustomMessageBox::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_dragging = true;
        m_dragStartPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void CustomMessageBox::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton && m_dragging) {
        move(event->globalPos() - m_dragStartPosition);
        event->accept();
    }
}

void CustomMessageBox::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_dragging = false;
        event->accept();
    }
}

// 静态方法实现
int CustomMessageBox::information(QWidget *parent, const QString &title, const QString &text)
{
    CustomMessageBox msgBox(parent);
    msgBox.setTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(Information);
    msgBox.setButtons(Ok);
    msgBox.setDefaultButton(Ok);
    return msgBox.exec();
}

int CustomMessageBox::warning(QWidget *parent, const QString &title, const QString &text)
{
    CustomMessageBox msgBox(parent);
    msgBox.setTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(Warning);
    msgBox.setButtons(Ok);
    msgBox.setDefaultButton(Ok);
    return msgBox.exec();
}

int CustomMessageBox::critical(QWidget *parent, const QString &title, const QString &text)
{
    CustomMessageBox msgBox(parent);
    msgBox.setTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(Critical);
    msgBox.setButtons(Ok);
    msgBox.setDefaultButton(Ok);
    return msgBox.exec();
}

int CustomMessageBox::question(QWidget *parent, const QString &title, const QString &text)
{
    CustomMessageBox msgBox(parent);
    msgBox.setTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(Question);
    msgBox.setButtons(Yes | No);
    msgBox.setDefaultButton(No);
    return msgBox.exec();
}

int CustomMessageBox::question(QWidget *parent, const QString &title, const QString &text, 
                              ButtonType yesButton, ButtonType noButton)
{
    CustomMessageBox msgBox(parent);
    msgBox.setTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(Question);
    msgBox.setButtons(yesButton | noButton);
    msgBox.setDefaultButton(noButton);
    return msgBox.exec();
}
